package cn.jdy.plugins.wap;


import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import cn.jdy.common.ctrl.BaseCtrl;
import cn.jdy.framework.log.BaseLog;
import cn.jdy.framework.util.DateUtil;
import cn.jdy.plugins.entity.Collect;
import cn.jdy.plugins.entity.CollectPK;
import cn.jdy.plugins.entity.Member;
import cn.jdy.plugins.service.CollectService;

/**
 * 会员收藏控制类
 */
@Controller
@RequestMapping("/collect")
public class CollectCtrl extends BaseCtrl {
	@Autowired
	private CollectService collectService;

	/**
	 * 查询订单
	 */
	@RequestMapping("/list")
	public ModelAndView list(Collect collect, String fromDate, String toDate) {
		ModelAndView mv = new ModelAndView(super.t + "/my/myshoucang");
		try {
			//设置分页信息
			if(collect.getiDisplayLength() == 0) collect.setiDisplayLength(5);                //默认长度5
			if(collect.getPageNo()<=0) collect.setPageNo(1);                                  //默认页数1
			collect.setiDisplayStart((collect.getPageNo() - 1) * collect.getiDisplayLength());  //显示起始索引

			mv.addAllObjects(collectService.query(collect, super.getMember().getId(), fromDate, toDate));
			mv.addObject("pageNo", collect.getPageNo());
			mv.addObject("fromDate", fromDate);
			mv.addObject("toDate", toDate);
		} catch(Exception e) {
			BaseLog.e(this.getClass(), "查询我的收藏信息错误", e);
		}
		return mv;
	}

	/**
	 * 删除我的收藏信息
	 *
	 * @param businessId
	 * @return
	 */
	@RequestMapping("/delete/{businessId}")
	public ModelAndView delete(@PathVariable String businessId) {
		ModelAndView mv = new ModelAndView("redirect:/collect/list");
		collectService.delete(new CollectPK(super.getMember().getId(), businessId));
		return mv;
	}

	/**
	 * 添加我的收藏
	 *
	 * @param businessId
	 * @return
	 */
	@RequestMapping("/save")
	@ResponseBody
	public Map<String, Object> save(String businessId) {
		Map<String, Object> jsonMap = new HashMap<String, Object>();
		try {
			Member menber = super.getMember();
			Collect collect = new Collect();
			collect.setId(new CollectPK(menber.getId(), businessId));
			collect.setCreateTime(DateUtil.getCurrentTime());
			collectService.save(collect);
			jsonMap.put("statusCode", 200);
		} catch(Exception e) {
			jsonMap.put("statusCode", 500);
			BaseLog.e(this.getClass(), "查询错误", e);
		}
		return jsonMap;
	}

}